﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharePointClient.SPWebService.SiteData;
using SharePointClient.SPWebService.Webs;

namespace SharePointClient
{
    public class SiteCollection:IDisposable,IEnumerable<Site>
    {

        List<Site> sites = new List<Site>();
        internal SiteCollection(Connection myConnection,_sWebWithTime[] vWebs)
        {
            foreach (_sWebWithTime url in vWebs)
            {
                sites.Add(myConnection.Open(url.Url));
            }
        }
        internal SiteCollection(Connection myConnection, string[] titles, string [] urls)
        {
            for (int i = 0; i < titles.Length && i < urls.Length; i++)
            {
                Site site;
                Uri uri=new Uri(urls[i]);
                if (myConnection.Cached(uri)) site = (Site) myConnection[uri];
                else site = new Site(myConnection, uri, titles[i]);
                sites.Add(site);
            }
        }

        public Site this[int pos]
        {
            get { return sites[pos];}
        }
        public int Count { get { return sites.Count; } }


        #region IDisposable Member

        public void Dispose()
        {
            foreach (Site s in sites) s.Dispose();
            sites.Clear();
        }

        #endregion




        #region IEnumerable<Site> Member

        public IEnumerator<Site> GetEnumerator()
        {
            return sites.GetEnumerator();
        }

        #endregion

        #region IEnumerable Member

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return sites.GetEnumerator();
        }

        #endregion
    }
}
